<?php 
/*
	本类用来封装PDO
	param1 数据库类型
	param2 数据库地址
	param3 数据库名称
	param4 数据库用户名
	param5 数据库密码
 */
class PDOMethod
{
	private $pdo;
	private $dbType;
	private $dbHost;
	private $dbName;
	private $dbUser;
	private $dbPass;
	//构造方法，用来实例化PDO类
	function __construct($dbType,$dbHost,$dbName,$dbUser,$dbPass)
	{
		$this->dbUser = $dbUser;
		$this->dbHost = $dbUser;
		$this->dbName = $dbUser;
		$this->dbUser = $dbUser;
		$this->dbPass = $dbUser;
		try {
			$this->pdo = new PDO("$dbType:host=$dbHost;dbname=$dbName;",$dbUser,$dbPass);
		} catch (PDOException $e) {
			return "链接错误";	
		}
	}
	//查询
	public function getData($table,$where='1=1')
	{
		$stmt = $this->pdo->query("select * from $table where $where");
		$data = $stmt->fetchAll(2);
		$this->pdo->setAttriBute(3,1);
		return $data;
	}
	//删除
	public function del($table,$where)
	{
		if (empty($where)) {
			return "条件不能为空";
		}
		$res = $this->pdo->exec("delete from $table where $where");
		if ($res >0) {
			return "删除成功";
		}else{
			return "删除失败";
		}
	}
	//添加
	public function add($table,$val)
	{
		$res = $this->pdo->exec("insert into $table values($val)");
		if ($res >0) {
			return "添加成功";
		}else{
			return "添加失败";
		}
	}
	//修改
	public function update($table,$where,$val)
	{
		if (empty($where)) {
			return "条件不能为空";
		}
		$res = $this->pdo->exec("update $table set $val where $where");
		if ($res >0) {
			return "修改成功";
		}else{
			return "修改失败";
		}
	}
}
